<template>
	<!-- 版本更新 -->
	<uni-popup ref="popup_update" @click.stop="back_update()">
		<view class="version-update">
			<view class="tip">版本更新</view>
			<view class="sub_tip" v-if="systemType == 'ios'">新版本{{ latestVersion }}已经发布，请在苹果商店下载我们最新的版本。</view>
			<view class="sub_tip2">本次更新内容:</view>
			<view class="msg-box">
				<rich-text :nodes="upApp"></rich-text>
			</view>
			<view class="btn-group">
				<button type="default" class="back-btn" @click="back_update()">取消</button>
				<button type="default" class="sue-btn" @click="go_update()">{{ systemType == 'android' ? '立即更新' : '知道了' }}</button>
			</view>
		</view>
	</uni-popup>
</template>

<script>
import config from '@/common/js/config.js';
import diyRichText from '@/components/diy-rich-text/diy-rich-text.vue';

export default {
	name: 'ns-version-update',
	props: {
		
	},
	mixins: [],
	data() {
		return {
			//隔多长时间显示一次更新信息
			showUpdateInfoInterval :1000 * 3600 * 6,
			upApp:'',
			systemType:'',//系统类型
			latestVersion:'',
		};
	},
	created() {
		// #ifdef APP-PLUS
		let that = this;
		uni.removeStorageSync('versionUpdateAction');
		that.checkVersionUpdate();
		uni.$on('versionUpdateCheck', function(data){
			that.checkVersionUpdate();
		})
		that.systemType = uni.getSystemInfoSync().platform;
		// #endif
	},
	methods: {
		// 版本更新
		async checkVersionUpdate() {
			let version = config.version_code;
			let versionUpdateAction = uni.getStorageSync('versionUpdateAction');
			if(versionUpdateAction && versionUpdateAction.version_code == version){
				let time = (new Date()).getTime();
				if(time - versionUpdateAction.action_time < this.showUpdateInfoInterval){
					return;
				}
			}
			switch (uni.getSystemInfoSync().platform) {
				case 'android':
					this.$api.sendRequest({
						url: '/appversion/api/version/latestInfo',
						data: {
							system_type: "Android"
						},
						success: res => {
							if (res.data && res.data.version_code > version) {
								this.latestVersion = res.data.version_code;
								// 升级提示
								this.upApp = res.data.update_log
								this.upCode = res.data.version_code
								this.$refs.popup_update.open()
							}
						}
					});
					break;
				case 'ios':
					this.$api.sendRequest({
						url: '/appversion/api/version/latestInfo',
						data: {
							system_type: "IOS"
						},
						success: res => {
							//console.log(res)
							if (res.data && res.data.version_code > version) {
								this.latestVersion = res.data.version_code;
								// 升级提示
								this.upApp = res.data.update_log
								this.$refs.popup_update.open()
							}
						}
					});
					break;
				default:
			}
		},
		back_update() {
			this.recordUpdateAction('cancel');
			this.$refs.popup_update.close()
		},
		go_update() {
			this.recordUpdateAction('confirm');
			
			this.$refs.popup_update.close()
			// 跳外网
			if (uni.getSystemInfoSync().platform == "android") {
				this.$api.sendRequest({
					url: '/appversion/api/version/info',
					data: {
						system_type: "Android",
						version_code: this.upCode
					},
					success: res => {
					
						plus.runtime.openURL(res.data.real_download_url);
						// console.log(res.data.download_url)
					}
				});
			} else {
				//plus.runtime.openURL(config.appleUrl)
				// console.log(config.appleUrl)
			}
		},
		close_update(){
			this.recordUpdateAction('cancel');
		},
		//记录升级操作时间
		recordUpdateAction(action_type){
			let version_code = config.version_code;
			uni.setStorageSync('versionUpdateAction', {
				action_type : action_type,
				version_code : version_code,
				action_time : (new Date()).getTime(),
			});
		}
	}
};
</script>

<style lang="scss">
	 .version-update {
	 	box-sizing: border-box;
	 
	 	.tip {
	 		padding: 30rpx;
	 		padding-bottom: 50rpx;
	 		text-align: center;
	 		font-size: 35rpx;
	 		color: #303133;
	 
	 	}
	 
	 	.sub_tip {
	 		color: #303133;
	 		text-align: left;
	 		padding: 0 30rpx;
	 		font-size: 30rpx;
	 	}
	 
	 	.sub_tip2 {
	 		padding: 0 30rpx;
	 		color: #303133;
	 		text-align: left;
	 		font-size: 30rpx;
	 	}
	 
	 	.msg-box {
	 		padding: 0 30rpx;
	 	}
	 
	 	.btn-group {
	 		display: flex;
	 		align-items: center;
	 		margin: 0 30rpx 35rpx;
	 		margin-top: 60rpx;
	 
	 		button {
	 			font-size: 30rpx;
	 			letter-spacing: 1rpx;
	 			width: 320rpx;
	 			height: 80rpx;
	 			color: #000000;
	 			border-radius: 15rpx;
	 			margin: 0 15rpx;
	 			line-height: 80rpx;
	 		}
	 
	 		.back-btn {
	 			background-color: white;
	 			border: 1rpx solid #AAAAAA;
	 		}
	 
	 		.sue-btn {
	 			background-color: $base-color;
	 			border: none;
	 		}
	 	}
	 
	 }
</style>
